package love.yuanmengrobot.als.business.dao.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:Lishao
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/10 21:11
 */

import love.yuanmengrobot.als.business.dao.StudentCreditScoreDao;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.regex.Pattern;

public class StudentCreditScoreDaoImpl {
    public static HashMap studentViewCreditgetStudentCredits(String id, String name){
        HashMap<Double,String> studentViewCredit=new HashMap<Double,String>();
        double credit_agg=0;
        double credit_score=0;
        double creditscore=0;
        Connection conn;
        PreparedStatement pstmt;
        String sql = " select credit,score from score where course_type in ('通识必修','学科基础','实践环节','专业基础必修','专业限选') " +
                "and student_id = (select student_id from student where stu_no =? and stu_name=?)";
        ResultSet rs ;
        try {
            conn = JdbcUtil.getConnection();//拿db连接
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);//参数设置
            pstmt.setString(2,name);
            rs = pstmt.executeQuery();//执行查询
            int scoreint=0;
            while (rs.next()){
                float credit=rs.getFloat(1);
                String scorestring=rs.getString(2);
                if(StudentCreditScoreDaoImpl.isInteger(scorestring)){
                    scoreint=Integer.parseInt(scorestring);
                }
                else{
                    if ( "旷考".equals(scorestring) || "取消".equals(scorestring)){
                        scoreint=0;
                    }
                    if ("不及格".equals(scorestring)|| "不合格".equals(scorestring)){
                        scoreint=45;
                    }
                    if("合格".equals(scorestring)|| "及格".equals(scorestring)){
                        scoreint=65;
                    }
                    if("中".equals(scorestring)){
                        scoreint=75;
                    }
                    if("良".equals(scorestring)){
                        scoreint=85;
                    }
                    if("优".equals(scorestring)){
                        scoreint=95;
                    }
                }
                credit_score=credit_score+credit*scoreint;
                credit_agg=credit_agg+credit;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        creditscore=credit_score/credit_agg;
        System.out.println(credit_agg);
        System.out.println(credit_score);
        studentViewCredit.put(creditscore,name);
        System.out.println(studentViewCredit);
        return studentViewCredit;
    }
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

}
